/*
 * File: ttds_timer.h
 * Author: Liang YaoZhan <262666882@qq.com>
 * Brief:  timer
 *
 * Copyright (c) 2022 - 2023 Liang YaoZhan <262666882@qq.com>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * History:
 * ================================================================
 * 2023-08-05 Liang YaoZhan <262666882@qq.com> created
 *
 */

#ifndef ttds_TIMER_H
#define ttds_TIMER_H

#include "ttds/ttds_queue.h"

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct ttds_timer_s ttds_timer_t;
struct ttds_tick_s;
typedef struct ttds_tick_s ttds_tick_t;

struct ttds_timer_s
{
/* private: */
    ttds_queue_t       node;
    unsigned int      tick_origin;
    int               tick;
    ttds_tick_t     *p_ticker;

/* public: */
    void (*timeout_callback)( ttds_timer_t *p_this, void *arg); /*!< timeout callback function */
    void             *arg;
};

void ttds_timer_init( ttds_timer_t *p_this, void (*cb)(ttds_timer_t *p_this, void*arg), void *arg);
void ttds_timer_destroy( ttds_timer_t *p_this );
void ttds_timer_restart(  ttds_timer_t *p_timer );

struct ttds_tick_s
{
    ttds_queue_t head;
};

void ttds_tick_init(  ttds_tick_t *p_this);
void ttds_tick_increase(  ttds_tick_t *p_this, int tick );
void ttds_tick_destroy(  ttds_tick_t *p_this);

void ttds_tick_add_timer(  ttds_tick_t *p_this, ttds_timer_t *p_timer, int tick);
void ttds_tick_remove_timer(  ttds_tick_t *p_this, ttds_timer_t *p_timer);
int  ttds_tick_next(ttds_tick_t *p_this);

#ifdef __cplusplus
}
#endif


#endif

